$(function(){
    let $user_name = $("#user_name");
    let $password  = $("#password")
    $("#btn_login").click(function(){
        let options={
             url:"http://127.0.0.1:8888/users/login",
             type:"POST",
             data:{
                username:$user_name.val(),
                password:$password.val()
             },
             datatype:"json"
        }
        $.ajax(options)
        .done(function(data){
             console.log(data);
             switch(data.code){
                  case 0:
                  error_logion(data.message)
                  break;
                  case 1:
                  success_logion(data.message)
                  setCookie("user_id",data.user.id);
                  setCookie("token",data.token,{
                      expires:1
                  });

             }
        })
        //警示框
        function error_logion(word){
            $("#login").css({display:"block"}).removeClass("alert-success").addClass("alert-danger").html(word)
            setTimeout(function(){
                $("#login").css({display:"none"})
            },2000)
        }
        function success_logion(word){
            $("#login").css({display:"block"}).removeClass("alert-danger").addClass("alert-success").html(word)
            setTimeout(function(){
                $("#login").css({display:"none"})
                if(location.hash==="#photo"){
                    history.go(-1)
                
                }else if(location.hash==="#goodslist"){
                    history.go(-1)
                }else{
                    location.href="../../dist/index.html"
                }
            },2000)
        }

    })
})